<template>

  <el-row type="flex"  justify="center" class="row-bg">
    <el-col :span="6">
      <div class="login">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-loginForm">
          <el-form-item label="用户名" prop="adminName">
            <el-input v-model="loginForm.adminName"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="adminPwd">
            <el-input type="password" v-model="loginForm.adminPwd"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import router from "../../router";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        adminName: '',
        adminPwd:''
      },
      rules: {
        adminName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        adminPwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("/admin/login",this.loginForm).then(res => {
            if(res.data.code == 0){
              router.push("/admin/main")
            }
            else if(res.data.code == 801) {
              this.$message.error(res.data.message);
            }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

.el-row {
  background-color: #fafafa;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
}
</style>